package com.svms.vehicle;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.svms.service.constants.Constants;
import com.svms.service.search.SearchOperators;
import com.svms.service.vo.ResultCodeVO;
import com.svms.service.vo.TransportVO;
import com.svms.service.vo.VehicleVO;
import com.svms.transport.TransportListModelItemRender;
import com.svms.utils.SvmsMessageUtils;
import com.svms.utils.SvmsWindowUtils;
import com.svms.web.factory.SvmsWebFactory;
import com.svms.web.search.SearchListModelItemRender;

public class VehicleSearchDialogueCtrl extends GenericForwardComposer {

	private Window window_vehicle_search;

	private Textbox txb_vehicle_model;
	private Datebox dtb_Frm47;
	private Datebox dtb_NC;
	private Datebox dtb_FC;
	private Datebox dtb_Insurance;
	private Datebox dtb_Tax;

	private Listbox lstbx_transport;
	private Listbox lstbx_vehicles;

	private Listbox lstbx_Operator_Frm47;
	private Listbox lstbx_Operator_NC;
	private Listbox lstbx_Operator_FC;
	private Listbox lstbx_Operator_Insurance;
	private Listbox lstbx_Operator_Tax;

	private Button btn_search;
	private Button btn_close;

	private ManageVehicleCtrl manageVehicleCtrl;

	private ListModelList<VehicleVO> lst_model_vehicles;
	private ListModelList<TransportVO> lst_model_transport;

	private ListModelList<SearchOperators> lst_model_search_operators;

	private VehicleVO vehicleVO;
	private VehicleVO oldvehicleVO;

	public VehicleSearchDialogueCtrl() {
		super();
	}

	/**
	 * Populates the vehicle details list to the drospdown on load of window
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onCreate$window_vehicle_search(Event event) throws Exception {

		Map<String, Object> args = SvmsWindowUtils.getCreationArgsMap(event);
		manageVehicleCtrl = (ManageVehicleCtrl) args.get("vehicleListCtrl");

		lst_model_vehicles = new ListModelList<VehicleVO>(SvmsWebFactory
				.vehicleService().getVehicleList());
		lst_model_transport = new ListModelList<TransportVO>(SvmsWebFactory
				.transportService().getTransportList());

		lstbx_vehicles.setModel(lst_model_vehicles);
		lstbx_vehicles.setItemRenderer(new ListitemRenderer() {
			@Override
			public void render(Listitem item, Object data, int index)
					throws Exception {

				final VehicleVO vehicle = (VehicleVO) data;
				Listcell lc = new Listcell(vehicle.getVehicleNumber());
				lc.setParent(item);
				item.setAttribute("selectedVehicle", data);

			}
		});

		lstbx_transport.setModel(lst_model_transport);
		lstbx_transport.setItemRenderer(new TransportListModelItemRender());

		lst_model_search_operators = new ListModelList<SearchOperators>(
				SearchOperators.getAllOperators());

		lstbx_Operator_Frm47.setModel(lst_model_search_operators);
		lstbx_Operator_Frm47.setItemRenderer(new SearchListModelItemRender());

		lstbx_Operator_FC.setModel(lst_model_search_operators);
		lstbx_Operator_FC.setItemRenderer(new SearchListModelItemRender());

		lstbx_Operator_NC.setModel(lst_model_search_operators);
		lstbx_Operator_NC.setItemRenderer(new SearchListModelItemRender());

		lstbx_Operator_Insurance.setModel(lst_model_search_operators);
		lstbx_Operator_Insurance
				.setItemRenderer(new SearchListModelItemRender());

		lstbx_Operator_Tax.setModel(lst_model_search_operators);
		lstbx_Operator_Tax.setItemRenderer(new SearchListModelItemRender());

		enableAllInputFields();
		enableAllButtons();

		vehicleVO = new VehicleVO();
		oldvehicleVO = new VehicleVO();

	}

	private void enableAllInputFields() {
		lstbx_transport.setDisabled(false);
		lstbx_vehicles.setDisabled(false);
		txb_vehicle_model.setDisabled(false);
		lstbx_Operator_Frm47.setDisabled(false);
		lstbx_Operator_NC.setDisabled(false);
		lstbx_Operator_FC.setDisabled(false);
		lstbx_Operator_Insurance.setDisabled(false);
		lstbx_Operator_Tax.setDisabled(false);
	}

	private void enableAllButtons() {
		btn_search.setDisabled(false);
		btn_close.setDisabled(false);
	}

	/**
	 * Used to search the vehicle for the given criteria and populate the Main
	 * Vehicles List
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_search(Event event) throws Exception {

		System.out.println("Entering into vehicles search");

		writeDialogueToBean();

		Map<String, SearchOperators> operatorsMap = writeSearchOperatorsToBean();

		List<VehicleVO> vehicles = SvmsWebFactory.vehicleService()
				.searchVehicle(vehicleVO, operatorsMap);

		if (vehicles != null && vehicles.size() == 1) {
			ResultCodeVO resultCodeVO = vehicles.get(0).getResultCode();
			if (resultCodeVO != null
					&& resultCodeVO.getErrorDetails().size() > 0) {
				SvmsMessageUtils.showErrorMessageFromLable(resultCodeVO
						.getErrorDetails());
			}
		}

		System.out.println("Searching for vehicles");
		System.out.println("Search Results ==> " + vehicles);

		if (manageVehicleCtrl.getLstbx_vehicles() != null) {
			manageVehicleCtrl.getLstbx_vehicles().setModel(
					new ListModelList(vehicles));
		}
		// Detach window after search
		window_vehicle_search.detach();
		System.out.println("Vehicles found");

	}

	/**
	 * Close the search window
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_close(Event event) throws Exception {
		window_vehicle_search.detach();
	}

	private void writeDialogueToBean() {

		Listitem listitem = lstbx_vehicles.getSelectedItem();
		if (listitem != null) {
			VehicleVO selectedVehile = (VehicleVO) listitem
					.getAttribute("selectedVehicle");
			if (selectedVehile != null) {
				vehicleVO.setVehicleId(selectedVehile.getVehicleId());
				vehicleVO.setVehicleNumber(selectedVehile.getVehicleNumber());
			}
		}

		Listitem transportListItem = lstbx_transport.getSelectedItem();
		if (transportListItem != null) {
			TransportVO selecteTransport = (TransportVO) transportListItem
					.getAttribute("selectedTransport");
			System.out.println("selecteTransport==>" + selecteTransport);
			if (selecteTransport != null) {
				vehicleVO.setTransport(selecteTransport);
			}
		}

		if (txb_vehicle_model.getValue() != null) {
			vehicleVO.setModel("%" + txb_vehicle_model.getValue() + "%");
		}

		Map<String, Date> vehicleDocuments = new HashMap<String, Date>();
		vehicleDocuments.put(Constants.FORM47_TD, dtb_Frm47.getValue());
		vehicleDocuments.put(Constants.NC_TD, dtb_NC.getValue());
		vehicleDocuments.put(Constants.FC_TD, dtb_FC.getValue());
		vehicleDocuments.put(Constants.INSURANCE_TD, dtb_Insurance.getValue());
		vehicleDocuments.put(Constants.TAX_TD, dtb_Tax.getValue());

		vehicleVO.setVehicleDocuments(vehicleDocuments);

	}

	private Map<String, SearchOperators> writeSearchOperatorsToBean() {

		Map<String, SearchOperators> operatorsMap = new HashMap<String, SearchOperators>();

		Listitem operatorForm47 = lstbx_Operator_Frm47.getSelectedItem();
		if (operatorForm47 != null) {
			SearchOperators selectedOperation = (SearchOperators) operatorForm47
					.getAttribute("selectedOperator");
			operatorsMap.put(Constants.FORM47_TD_OPERATOR, selectedOperation);

		}
		Listitem operatorFC = lstbx_Operator_FC.getSelectedItem();
		if (operatorFC != null) {
			SearchOperators selectedOperation = (SearchOperators) operatorFC
					.getAttribute("selectedOperator");
			operatorsMap.put(Constants.FC_TD_OPERATOR, selectedOperation);

		}
		Listitem operatorNC = lstbx_Operator_NC.getSelectedItem();
		if (operatorNC != null) {
			SearchOperators selectedOperation = (SearchOperators) operatorNC
					.getAttribute("selectedOperator");
			operatorsMap.put(Constants.NC_TD_OPERATOR, selectedOperation);

		}
		Listitem operatorInsurance = lstbx_Operator_Insurance.getSelectedItem();
		if (operatorInsurance != null) {
			SearchOperators selectedOperation = (SearchOperators) operatorInsurance
					.getAttribute("selectedOperator");
			operatorsMap
					.put(Constants.INSURANCE_TD_OPERATOR, selectedOperation);

		}
		Listitem operatorTax = lstbx_Operator_Tax.getSelectedItem();
		if (operatorTax != null) {
			SearchOperators selectedOperation = (SearchOperators) operatorTax
					.getAttribute("selectedOperator");
			operatorsMap.put(Constants.TAX_TD_OPERATOR, selectedOperation);
		}
		return operatorsMap;
	}
}
